package org.itboys.store.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.itboys.commons.utils.io.FileUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.itboys.oss.OssService;
import org.itboys.store.cache.BrandLoader;
import org.itboys.store.constant.ProductConstant;
import org.itboys.store.entity.ProductBrand;
import org.itboys.store.entity.ProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import org.mongodb.morphia.query.Query;

/**
 * 商品品牌
 * @author weisky
 *
 */
@Service
public class StoreProductBrandService extends BaseService<ProductBrand, Long>{

	private static final long serialVersionUID = -6789423250473830602L;
	
	@Autowired
	private ResourceHolder resourceHolder;
	
	@Resource(name = "productDS")
	private MongoDataSource ds;
	@Resource(name = "ossService")
	OssService productOssService;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<ProductBrand> getEntityClass() {
		return ProductBrand.class;
	}
	
	public List<ProductBrand> searchKeyWords(String keyWords){
		Query<ProductBrand> query = ds.createQuery(ProductBrand.class).field("name").contains(keyWords);
		query.filter("status", 1);
		return query.asList();
	}
	
	/**
	 * 保存或者修改
	 * @param productBrand
	 * @param tlogo
	 * @return
	 */
	public boolean insert(ProductBrand productBrand, MultipartFile tlogo){
		try{
			String tempFile = null;
			if (tlogo != null && !tlogo.isEmpty() && tlogo.getSize() > 0) {
				tempFile = productOssService.upload(tlogo.getInputStream(), ProductConstant.ImgPath.OSS_PATH);
				productBrand.setLogo(ProductConstant.OSS_URL_PREFIX + ProductConstant.ImgPath.OSS_PATH + "/" + tempFile);
			}else{	
				if(productBrand.getId() != 0){
					ProductBrand productBrandT = getById(productBrand.getId());
					productBrand.setLogo(productBrandT.getLogo());
				}
			}
			if(productBrand.getId() != 0){
				update(productBrand);
			}else{
				save(productBrand);
			}
			return true;
		}catch(Exception e){
			logger.error("ProductBrandService.insert error", e);
			e.printStackTrace();
		}
		
		return false;
	}
	
	public boolean dele(Long id){
		try{
			delete(id);
			return true;
		}catch(Exception e){
			logger.error("ProductBrandService.dele error", e);
			e.printStackTrace();
		}
		return false;
	}
	public List<ProductBrand> list(HttpServletRequest request) {
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		Iterator<String> ite = param.keySet().iterator();
		Query<ProductBrand> query = ds.createQuery(getEntityClass()).filter(
				"isDeleted", 0);
		while (ite.hasNext()) {
			String key = ite.next();
			Object value = param.get(key);
			query.filter(key, value);
		}
		List<ProductBrand> amList = query.asList();
//		for(ProductBrand pc : amList){
//			pc.set_parentId(pc.getParentId());
//		}
		return amList;
	}
}
